package com.youthburin.rmp.services.impl;

import java.util.Date;
import java.util.List;

import net.sf.ehcache.CacheManager;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.youthburin.base.domain.UserInfo;
import com.youthburin.base.services.AbstractService;
import com.youthburin.base.springjpa.MyRepository;
import com.youthburin.rmp.domain.RmpNotice;
import com.youthburin.rmp.repositories.RmpNoticeRepository;
import com.youthburin.rmp.services.RmpNoticeService;

@Service
public class RmpNoticeServiceImpl extends AbstractService<RmpNotice, String> implements RmpNoticeService {
	@Autowired
	private RmpNoticeRepository rmpNoticeRepository;
	@Autowired
	private CacheManager cacheManager;

	@Override
	public MyRepository<RmpNotice, String> getMyRepository() {
		return rmpNoticeRepository;
	}

	@Override
	public RmpNotice save(RmpNotice notice) {
		UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
		notice.setCreater(user.getCode());
		notice.setCreateTime(new Date());

		cacheManager.getCache("rmp-notice-list-cache").removeAll();
		return rmpNoticeRepository.save(notice);
	}

	@Override
	public RmpNotice update(RmpNotice notice) {
		UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
		notice.setUpdater(user.getCode());
		notice.setUpdateTime(new Date());

		cacheManager.getCache("rmp-notice-list-cache").removeAll();
		return rmpNoticeRepository.update(notice);
	}

	@Override
	@Cacheable("rmp-notice-list-cache")
	public List<RmpNotice> findAll(Sort sort) {
		return rmpNoticeRepository.findAll(sort);
	}

	@Override
	public void delete(List<String> idList) {
		cacheManager.getCache("rmp-notice-list-cache").removeAll();
		super.delete(idList);
	}

}
